package com.kongshidian.ui;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import com.kongshidian.R;
import com.kongshidian.ui.fragment.AddKSDFragment;
import com.kongshidian.ui.fragment.AllKSDFragment;
import com.kongshidian.ui.fragment.TopKSDFragment;
import com.kongshidian.ui.view.PagerSlidingTabStripView;
import com.kongshidian.util.ContextUtil;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;


/**
 *  空时店首页
 * @author ghg
 *
 */
public class KSDMainActivity extends BaseActivity{
	
	@ViewInject(id = R.id.main_search_edit, click = "butClick") 
	EditText mSearchEdit;
	
	/**
	 * PagerSlidingTabStrip的实例
	 * 
	 */
	@ViewInject(id=R.id.tabs)
	private PagerSlidingTabStripView tabs;
	/**
	 * 获取当前屏幕的密度
	 */
	private DisplayMetrics dm;
	
	/**
	 * 选项内容页
	 */
	@ViewInject(id = R.id.pager)
	ViewPager pager;
	
	
	/**
	 * 顶级空时店
	 */
	private TopKSDFragment topKSDFragment;
	
	
	/**
	 * 顶级空时店
	 */
	private AllKSDFragment allKSDFragment;
	
	/**
	 * 顶级空时店
	 */
	private AddKSDFragment addKSDFragment;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.activity_ksdmain);
		FinalActivity.initInjectedView(this); //置于setContentView之后,否则会空指针
		

		
		dm = getResources().getDisplayMetrics();
		InitData();//事件
	}
	
	
	private void InitData() {
		pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		tabs.setViewPager(pager);
		setTabsValue();
	}

	/**
	 * 点击事件
	 */
	public void butClick(View v){
		//搜索
		if( v == mSearchEdit ){
			ContextUtil.startActivity(this, SearchActivity.class);
		}
	}
	
	
	/**
	 * 对PagerSlidingTabStrip的各项属性进行赋值。
	 */
	private void setTabsValue() {
		// 设置Tab是自动填充满屏幕的
		tabs.setShouldExpand(true);
		// 设置Tab的分割线是透明的
		tabs.setDividerColor(Color.TRANSPARENT);
		// 设置Tab底部线的高度
		tabs.setUnderlineHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 1, dm));
		// 设置Tab Indicator的高度
		tabs.setIndicatorHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, dm));
		// 设置Tab标题文字的大小
		tabs.setTextSize((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_SP, 14, dm));
		// 设置Tab Indicator的颜色
		tabs.setIndicatorColor(Color.parseColor("#FE6077"));
		// 设置选中Tab文字的颜色 (这是我自定义的一个方法)
		tabs.setSelectedTextColor(Color.parseColor("#FE6077"));
		// 取消点击Tab时的背景色
		tabs.setTabBackground(0);
	}
	
	
	
	
	public class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		private final String[] titles = { "顶级空时店", "所有空时店", "添加空时店" };

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				if ( topKSDFragment== null) {
					topKSDFragment = new TopKSDFragment();
				}
				return topKSDFragment;
			case 1:
				if (allKSDFragment == null) {
					allKSDFragment = new AllKSDFragment();
				}
				return allKSDFragment;
			case 2:
				if (addKSDFragment == null) {
					addKSDFragment = new AddKSDFragment();
				}
				return addKSDFragment;
			default:
				return null;
			}
		}

	}
}
